<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: absolute;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: skyblue;
        }

        span {
            /* 动画一定要加定位 */
            position: absolute;
            left: 0;
            top: 200px;
            display: block;
            width: 150px;
            height: 150px;
            background-color: purple;
        }
    </style>
</head>
<body>
    <button>开启</button>
    <div>一路向北</div>
    <span>龙卷风</span>

    <script>
          /* 
           需求 ： 
               1. span 自动往右移动，某个位置后停止
               2. 点击按钮后，div往右移动，某个位置后自动停止，
            解决方案 ： 写一个函数，元素和距离作为参数
        */

        let btn=document.querySelector('button')
        let div=document.querySelector('div')
        let span=document.querySelector('span')

        btn.addEventListener('click',function(){
        move (div,600)
        });
        function move (obj,target){
            let timer=setInterval(() => {
                let left=obj.offsetLeft;
                if(left>=target){
                    clearInterval(timer)
                    return
                }
                obj.style.left=left+50+'px'
            }, 200);
        }
        move(span,500)
    </script>
</body>
</html>